package com.example.smartwaterapi.config;

import com.example.smartwaterapi.entity.Device;
import com.example.smartwaterapi.entity.UserAchievement;
import com.example.smartwaterapi.entity.WaterRecord;
import com.example.smartwaterapi.entity.Watchface;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDate;
import java.util.Map;

/**
 * 缓存配置类
 */
@Configuration
public class CacheConfig {

    /**
     * 饮水记录缓存键生成器
     * 根据用户ID和日期生成缓存键
     */
    @Bean
    public KeyGenerator waterRecordCacheKeyGenerator() {
        return (target, method, params) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(method.getName()).append(":");
            
            for (Object param : params) {
                if (param instanceof WaterRecord) {
                    WaterRecord record = (WaterRecord) param;
                    sb.append("userId_").append(record.getUserId()).append("_");
                    if (record.getRecordDate() != null) {
                        sb.append("date_").append(record.getRecordDate());
                    } else {
                        LocalDate today = LocalDate.now();
                        sb.append("date_").append(today);
                    }
                } else if (param != null) {
                    sb.append(param.toString());
                }
            }
            
            return sb.toString();
        };
    }
    
    /**
     * 设备缓存键生成器
     */
    @Bean
    public KeyGenerator deviceCacheKeyGenerator() {
        return (target, method, params) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(method.getName()).append(":");
            
            for (Object param : params) {
                if (param instanceof Device) {
                    Device device = (Device) param;
                    if (device.getId() != null) {
                        sb.append("device_").append(device.getId());
                    } else if (device.getDeviceSn() != null) {
                        sb.append("sn_").append(device.getDeviceSn());
                    } else if (device.getUserId() != null) {
                        sb.append("userId_").append(device.getUserId());
                    }
                } else if (param instanceof String) {
                    sb.append("sn_").append(param);
                } else if (param instanceof Number) {
                    sb.append("id_").append(param);
                } else if (param != null) {
                    sb.append(param.toString());
                }
            }
            
            return sb.toString();
        };
    }
    
    /**
     * 用户成就缓存键生成器
     */
    @Bean
    public KeyGenerator achievementCacheKeyGenerator() {
        return (target, method, params) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(method.getName()).append(":");
            
            for (Object param : params) {
                if (param instanceof UserAchievement) {
                    UserAchievement achievement = (UserAchievement) param;
                    sb.append("user_").append(achievement.getUserId());
                } else if (param instanceof Number) {
                    sb.append("id_").append(param);
                } else if (param instanceof String) {
                    sb.append(param);
                } else if (param != null) {
                    sb.append(param.toString());
                }
            }
            
            return sb.toString();
        };
    }
    
    /**
     * 表盘缓存键生成器
     */
    @Bean
    public KeyGenerator watchfaceCacheKeyGenerator() {
        return (target, method, params) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(method.getName()).append(":");
            
            for (Object param : params) {
                if (param instanceof Watchface) {
                    Watchface watchface = (Watchface) param;
                    sb.append("watchface_").append(watchface.getId());
                } else if (param instanceof Number) {
                    sb.append("id_").append(param);
                } else if (param instanceof Map) {
                    sb.append("params_").append(param.hashCode());
                } else if (param != null) {
                    sb.append(param.toString());
                }
            }
            
            return sb.toString();
        };
    }
} 